package com.beyond.pratice.ui;

import com.beyond.pratice.model.User;
import com.beyond.pratice.service.UserService;

import javax.swing.*;
import javax.swing.table.*;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;

/**
 * User: lewisw
 * Date: 2008-3-6
 * Time: 9:47:20
 */
public class UserForm {

    static {
        try {
            UIManager.setLookAndFeel("com.sun.java.swing.plaf.windows.WindowsLookAndFeel");
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    private JTextField username;
    private JTextField mobileNo;
    private JTextField email;
    private JTable table1;
    private JPanel panel1;
    private JButton addButton;

    private UserService userService;

    public void setUserService(UserService userService) {
        this.userService = userService;
    }

    public UserForm() {

        addButton.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                addUser();
            }
        });
    }

    private void addUser() {
        User user = new User();
        getData(user);
        userService.save(user);
    }

    public void setData(User data) {
        username.setText(data.getUsername());
        mobileNo.setText(data.getMobileNo());
        email.setText(data.getEmail());
    }

    public void getData(User data) {
        data.setUsername(username.getText());
        data.setMobileNo(mobileNo.getText());
        data.setEmail(email.getText());
    }

    public boolean isModified(User data) {
        if (username.getText() != null ? !username.getText().equals(data.getUsername()) : data.getUsername() != null)
            return true;
        if (mobileNo.getText() != null ? !mobileNo.getText().equals(data.getMobileNo()) : data.getMobileNo() != null)
            return true;
        if (email.getText() != null ? !email.getText().equals(data.getEmail()) : data.getEmail() != null) return true;
        return false;
    }

    public void show() {
        JFrame frame = new JFrame("UserForm");
        frame.setContentPane(this.panel1);
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.pack();
        frame.setVisible(true);
    }

    private void createUIComponents() {
        table1 = new JTable(new DefaultTableModel(){

            public int getRowCount() {
                return 10;
            }

            public int getColumnCount() {
                return 3;
            }

            public String getColumnName(int columnIndex) {
                switch (columnIndex) {
                    case 0 :
                        return "User Name";
                    case 1 :
                        return "Mobile NO";
                    case 2 :
                        return "Email";
                }
                return null;
            }

            public Object getValueAt(int rowIndex, int columnIndex) {
                switch (columnIndex) {
                    case 0 :
                        return "lewis";
                    case 1 :
                        return "13575460141";
                    case 2 :
                        return "lewis.wang@infowarelab.com";
                }
                return null;
            }

        });
    }
}
